/*
 * @Author: your name
 * @Date: 2021-06-26 18:52:39
 * @LastEditTime: 2021-06-26 20:20:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \LinkList\100\100.cpp
 */
#include<queue>
#include<string>
#include <iostream>
// #include<set>
using namespace std ;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        queue<pair<TreeNode*,TreeNode*> > que;
        que.push(pair<TreeNode*,TreeNode*>(p,q));
        while(que.size()!=0)
        {
            auto ptr=que.front();
            que.pop();

            // 
            if((ptr.first==nullptr&&ptr.second==nullptr))
            continue;
            //jduge 

            if((ptr.first==nullptr&&ptr.second!=nullptr)||(ptr.first!=nullptr&&ptr.second==nullptr)||ptr.first->val !=ptr.second->val||(ptr.first->left==nullptr&&ptr.second->left!=nullptr)||(ptr.first->left!=nullptr&&ptr.second->left==nullptr)||(ptr.first->right!=nullptr&&ptr.second->right==nullptr)||(ptr.first->right==nullptr&&ptr.second->right!=nullptr))
                return false;
            //push

            if(ptr.first->left!=nullptr)
                que.push(pair<TreeNode*,TreeNode*>(ptr.first->left,ptr.second->left));
            if(ptr.first->right!=nullptr)
                que.push(pair<TreeNode*,TreeNode*>(ptr.first->right,ptr.second->right));
            
        }
        return true;
    }
};